package com.web.work;

import com.alibaba.fastjson2.JSON;
import com.web.JDBCUtils;
import com.web.Student;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

//2.搜索学生，传入关键字，搜索名字有该字的学生
@WebServlet("/two")
public class Test2 extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost( HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //设置内容类型
        resp.setContentType("application/json;charset=utf-8");
        String sql = "select * from student where name like ?";
        String keyword = req.getParameter("keyword");

        //查询学生信息
        List<Student> students= JDBCUtils.query(Student.class,sql,"%"+keyword+"%");
        //转换数据为JSON格式
        String str = JSON.toJSONString(students);
        resp.getWriter().write(str);
    }









}
